package com.apexwear.apexutils.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.RecyclerView;

import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnItemClickListener;
import com.apexwear.apexutils.model.HeartDevice;

import java.util.List;

public class AdapterHeartDeviceList extends RecyclerView.Adapter<AdapterHeartDeviceList.HeartDeviceHold> {
    Context mContext;
    List<HeartDevice> heartDeviceList;
    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public AdapterHeartDeviceList(Context mContext, List<HeartDevice> heartDeviceList) {
        this.mContext = mContext;
        this.heartDeviceList = heartDeviceList;
    }

    @NonNull
    @Override
    public HeartDeviceHold onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View adapterView = LayoutInflater.from(mContext).inflate(R.layout.adapter_heart_device_list, parent, false);
        return new HeartDeviceHold(adapterView);
    }

    @Override
    public void onBindViewHolder(@NonNull HeartDeviceHold holder, int position) {
        if (heartDeviceList == null || heartDeviceList.isEmpty()) {
            return;
        }
        HeartDevice heartDevice = heartDeviceList.get(position);
        holder.nameText.setText(heartDevice.getDeviceName());
        holder.stateText.setText(heartDevice.isConnectState() ? mContext.getString(R.string.connect) : mContext.getString(R.string.disconnect));
        holder.heartText.setText(heartDevice.getDeviceHeart() + "");
        holder.connectButton.setText(heartDevice.isConnectState() ? mContext.getString(R.string.disconnect) : mContext.getString(R.string.connect));
        holder.connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.clickListener(position);
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        return heartDeviceList == null ? 0 : heartDeviceList.size();
    }

    class HeartDeviceHold extends RecyclerView.ViewHolder {
        TextView nameText, stateText, heartText;
        AppCompatButton connectButton;

        public HeartDeviceHold(@NonNull View itemView) {
            super(itemView);
            nameText = itemView.findViewById(R.id.deviceName);
            stateText = itemView.findViewById(R.id.connect_state);
            connectButton = itemView.findViewById(R.id.connect_watch);
            heartText = itemView.findViewById(R.id.hear_rate);

        }
    }

    public void destroyAdapter() {
        mContext = null;
        if (heartDeviceList != null) {
            heartDeviceList.clear();
            heartDeviceList = null;
        }
        onItemClickListener = null;
    }
}
